/**判断是否为空*/
export function isEmpty(obj) {
	if (Array.isArray(obj)) {
		return obj.length === 0;
	} else if (Object.prototype.toString.call(obj) === '[object Object]') {
		return Object.keys(obj).length === 0;
	} else {
		return !obj;
	}
}
export function deepClone(source) {
	if (!source && typeof source !== 'object') {
		throw new Error('error arguments', 'deepClone');
	}
	const targetObj = source.constructor === Array ? [] : {};
	Object.keys(source).forEach(keys => {
		if (source[keys] && typeof source[keys] === 'object') {
			targetObj[keys] = deepClone(source[keys]);
		} else {
			targetObj[keys] = source[keys];
		}
	});
	return targetObj;
}
/**
 * @param {Function} func
 * @param {number} wait
 * @param {boolean} immediate
 * @return {*}
 */
export function debounce(func, wait, immediate) {
	let timeout, args, context, timestamp, result;

	const later = function () {
		// 据上一次触发时间间隔
		const last = +new Date() - timestamp;

		// 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
		if (last < wait && last > 0) {
			timeout = setTimeout(later, wait - last);
		} else {
			timeout = null;
			// 如果设定为immediate===true，因为开始边界已经调用过了此处无需调用
			if (!immediate) {
				result = func.apply(context, args);
				if (!timeout) context = args = null;
			}
		}
	};

	return function (...args) {
		context = this;
		timestamp = +new Date();
		const callNow = immediate && !timeout;
		// 如果延时不存在，重新设定延时
		if (!timeout) timeout = setTimeout(later, wait);
		if (callNow) {
			result = func.apply(context, args);
			context = args = null;
		}

		return result;
	};
}

export function format(date, format) {
	if (format === null || format === undefined || format === '') {
		format = 'yyyy-MM-dd hh:mm:ss';
	}
	if (date != 'Invalid Date') {
		var o = {
			'M+': date.getMonth() + 1, //month
			'd+': date.getDate(), //day
			'h+': date.getHours(), //hour
			'm+': date.getMinutes(), //minute
			's+': date.getSeconds(), //second
			'q+': Math.floor((date.getMonth() + 3) / 3), //quarter
			S: date.getMilliseconds() //millisecond
		};
		if (/(y+)/.test(format))
			format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
		for (var k in o)
			if (new RegExp('(' + k + ')').test(format))
				format = format.replace(
					RegExp.$1,
					RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
				);
		return format;
	}
	return '';
}

export function parseParams(paramStr) {
	let params = {};
	paramStr.split('&').forEach(param => {
		let [key, value] = param.split('=');
		params[key] = value;
	});
	return params;
}

/**按照英文排序*/
export function sortByEnglish(arr) {
	const str = 'ABCDEFGHJKLMNOPQRSTWXYZ#';
	const order = str.split(''); // 定义排序顺序
	return arr.sort((a, b) => {
		// 使用order数组找出每个元素的索引，并进行比较
		const indexA = order.indexOf(a);
		const indexB = order.indexOf(b);
		return indexA - indexB; // 返回差值，进行排序
	});
}

/**隐藏状态栏*/
export function hideTabBar() {
	uni.hideTabBar({
		animation: false
	});
	plus.navigator.setFullscreen(true);
}

/**展示状态栏*/
export function showTabBar() {
	uni.showTabBar({
		animation: false
	});
	plus.navigator.setFullscreen(false);
}

/**复制文字*/
export function duplicationText(text) {
	uni.setClipboardData({
		data: text,
		success: () => {
			uni.showToast({
				title: '复制成功',
				icon: 'none'
			});
		},
		fail: () => {
			uni.showToast({
				title: '复制失败',
				icon: 'none'
			});
		}
	});
}

// tabbar路由集合
const tabbarArr = [
	'/pages/index/index',
	'/pages/help/index',
	'/pages/work/index',
	'/pages/file/index',
	'/pages/phone/index'
];
/**路由跳转*/
export function jumpUrl(url, isNvue = false) {
	let urlArr = url.split('?');
	let page = urlArr[0];
	let isTabbar = tabbarArr.includes(page);
	// 是tabbar页就这样跳转
	if (isTabbar) {
		let params = urlArr[1];
		let obj = {};
		// 如果有参数解构参数
		if (params) {
			let paramsArr = params.split('&');
			paramsArr.forEach(item => {
				let key = item.split('=')[0];
				let value = item.split('=')[1];
				obj[key] = value;
			});
		}
		uni.switchTab({
			url: page,
			success: () => {
				// 二级nvue页面跳转即销毁，不能延时
				if (isNvue) {
					uni.setStorageSync('helpPageChange', true);
					uni.setStorageSync('helpPageChangeObj', obj);
					uni.$emit('helpPageChange', obj);
				} else {
					setTimeout(() => {
						uni.$emit('helpPageChange', obj);
					}, 500);
				}
			}
		});
	}
	// 其他页面跳转方式
	else {
		uni.navigateTo({
			url: url
		});
	}
}
